thin.imports('/public/configure/conf.minfo.js');
thin.imports('/public/configure/conf.opts.js');
thin.imports('/public/plug/FormCombox/FormCombox.js');
thin.imports('/public/plug/LoadEffect/LoadEffect.js');

thin.use(function() {
	// 頁面加載完畢
	thin.effect();

	layer.load(0, {
		shade: [0.08, '#000']
	});

	// 獲取投資喜好
	factory.request('_mlike_get');

	// 獲取opts
	__getdata_opts.name = 'round';
	factory.request('_common_opts', ['round', '投资轮次']);
	__getdata_opts.name = 'industry';
	factory.request('_common_opts', ['industry', '投资行业']);
	__getdata_opts.name = 'city';
	factory.request('_common_opts', ['city', '关注城市']);

	// 綁定提交按鈕
	$('#btn-like-submit').on('click', submitEditLike);

});

var __getdata_opts = {},
	data_opts = {}, // 记录opts的数据
	data_opts_index = 0, // 记录opts的次数
	data_like,
	__getdata_mlikecomplete = {};

// 获取投資喜好回调
function __complete_mlikeget(res) {
	if(res.status == 200) {
		data_like = res.data;
		vcre();
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 讀取opts回調
function __complete_opts(res, unkn) {
	data_opts_index = data_opts_index + 1;
	if(res.status == 200) {
		data_opts[unkn[0]] = [res.data, unkn[1]];
	} else {
		// nothing
	}
	// 判断是否全部读取了opts信息
	if(data_opts_index == 3) {
		vcre();
	}
}

// 判断是否全部读取了opts信息和投資喜好
function vcre() {
	if(data_like && data_opts_index == 3) {
		layer.closeAll('loading');
		createForm();
	}
}

// 渲染表单
function createForm() {
	var form = new FormCombox();
	var form_arr = [];
	for(var i in data_opts) {
		var data = data_opts[i][0];
		form_arr.push({
			'type': 'checkbox',
			'label': data_opts[i][1],
			'name': i,
			'text': '请选择',
			'value': data_like[i],
			'checkbox': data
		});
	}
	var ele = '';
	ele += '<input type="text" class="areat" name="begin_money" value="' + data_like.begin_money + '" />';
	ele += '<font class="p-x-2x">-</font>';
	ele += '<input type="text" class="areat" name="end_money" value="' + data_like.end_money + '" />';
	ele += '<font class="p-x-1x">万元</font>';
	form_arr.push({
		'type': 'custom',
		'label': '投资金额',
		'html': ele
	});
	form.addCombox(form_arr);
	form.reader('layout-form-like');
}

// 完善投資喜好
function submitEditLike() {
	var data_opt = {};
	$('#layout-form-like').find('input').each(function() {
		var name = this.name;
		if(name != '') data_opt[name] = this.value;
	});

	layer.load(0, {
		shade: [0.08, '#000']
	});

	__getdata_mlikecomplete = data_opt;
	factory.request('_mlike_complete');
}

// 完善投資喜好回調
function __complete_mlikecomplete(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		layer.msg('信息完善成功', {
			icon: 1,
			time: 1200
		}, function() {
			window.location.href = '/member/index.html';
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}